class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        // sort(intervals.begin(), intervals.end(), [&](vector<int> a, vector<int> b)
        // {
        //     return a[0] < b[0];
        // });
        sort(intervals.begin(), intervals.end());

        vector<vector<int>> ret;
        ret.push_back(intervals[0]);
        for (int i = 1; i < intervals.size(); i++)
        {
            vector<int>& back = ret.back();
            if (back[1] >= intervals[i][0])
            {
                if (back[1] <= intervals[i][1])
                    back[1] = intervals[i][1];
            }
            else
                ret.push_back(intervals[i]);
        }

        return ret;
    }
    
};